package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.core.domain.TreeLazySelect;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DicTreeSelect;
import com.ruoyi.system.domain.SysTreeDictType;
import com.ruoyi.system.mapper.SysTreeDictTypeMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTreeDictDataMapper;
import com.ruoyi.system.domain.SysTreeDictData;
import com.ruoyi.system.service.ISysTreeDictDataService;

/**
 * 字典数据Service业务层处理
 * 
 * @author genghz
 * @date 2021-11-17
 */
@Service
public class SysTreeDictDataServiceImpl implements ISysTreeDictDataService 
{
    @Autowired
    private SysTreeDictDataMapper sysTreeDictDataMapper;

    @Autowired
    private SysTreeDictTypeMapper sysTreeDictTypeMapper ;

    /**
     * 查询字典数据
     * 
     * @param dataId 字典数据主键
     * @return 字典数据
     */
    @Override
    public SysTreeDictData selectSysTreeDictDataByDataId(Long dataId)
    {
        return sysTreeDictDataMapper.selectSysTreeDictDataByDataId(dataId);
    }

    /**
     * 查询字典数据列表
     * 
     * @param sysTreeDictData 字典数据
     * @return 字典数据
     */
    @Override
    public List<SysTreeDictData> selectSysTreeDictDataList(SysTreeDictData sysTreeDictData)
    {
        List<SysTreeDictData> sysTreeDictDatas = sysTreeDictDataMapper.selectSysTreeDictDataList(sysTreeDictData);
        for (SysTreeDictData treeDictData : sysTreeDictDatas) {
            treeDictData.setHasChildren(!CollectionUtils.sizeIsEmpty(treeDictData.getChildren()));
        }
        return sysTreeDictDatas;
    }

    @Override
    public List<TreeLazySelect> buildTreeSelect(List<SysTreeDictData> sysTreeDictDataList) {
        return sysTreeDictDataList.stream().map(x-> {
            TreeLazySelect treeLazySelect = new TreeLazySelect();
            treeLazySelect.setId(x.getDataId());
            treeLazySelect.setLabel(x.getDictLabel());
            treeLazySelect.setHasChildren(CollectionUtils.isNotEmpty(x.getChildren()));
            return treeLazySelect ;
        }).collect(Collectors.toList());
    }

    /**
     * 新增字典数据
     * 
     * @param sysTreeDictData 字典数据
     * @return 结果
     */
    @Override
    public int insertSysTreeDictData(SysTreeDictData sysTreeDictData)
    {
        String prefixCode = "" ;
        SysTreeDictData parentDictData = sysTreeDictDataMapper.selectSysTreeDictDataByDataId(sysTreeDictData.getParentId());
        if(ObjectUtils.isNotEmpty(parentDictData)) {
            prefixCode = parentDictData.getDictCode();
        }

        String dictCode = prefixCode + RandomStringUtils.random(2, 65,90,true,true);
        while (sysTreeDictDataMapper.selectDataByCode(sysTreeDictData.getDictId(), dictCode) != null) {
            dictCode = prefixCode + RandomStringUtils.random(2, 65,90,true,true);
        }
        sysTreeDictData.setDictCode(dictCode);
        sysTreeDictData.setCreateTime(DateUtils.getNowDate());
        return sysTreeDictDataMapper.insertSysTreeDictData(sysTreeDictData);
    }

    /**
     * 修改字典数据
     * 
     * @param sysTreeDictData 字典数据
     * @return 结果
     */
    @Override
    public int updateSysTreeDictData(SysTreeDictData sysTreeDictData)
    {
        sysTreeDictData.setUpdateTime(DateUtils.getNowDate());
        return sysTreeDictDataMapper.updateSysTreeDictData(sysTreeDictData);
    }

    /**
     * 批量删除字典数据
     * 
     * @param dataIds 需要删除的字典数据主键
     * @return 结果
     */
    @Override
    public int deleteSysTreeDictDataByDataIds(Long[] dataIds)
    {
        for (Long dataId : dataIds) {
            SysTreeDictData sysTreeDictData = sysTreeDictDataMapper.selectSysTreeDictDataByDataId(dataId);
            if (ObjectUtils.isNotEmpty(sysTreeDictData) && CollectionUtils.isNotEmpty(sysTreeDictData.getChildren())) {
                throw new ServiceException("存在子节点，不允许删除");
            }
        }
        return sysTreeDictDataMapper.deleteSysTreeDictDataByDataIds(dataIds);
    }

    /**
     * 删除字典数据信息
     * 
     * @param dataId 字典数据主键
     * @return 结果
     */
    @Override
    public int deleteSysTreeDictDataByDataId(Long dataId)
    {
        return sysTreeDictDataMapper.deleteSysTreeDictDataByDataId(dataId);
    }

    @Override
    public List<DicTreeSelect> getSystemTreeData(String dictType) {
        List<SysTreeDictData> sysTreeDictDatas = sysTreeDictDataMapper.selectSysTreeDictDataListByDictType(dictType);
        List<SysTreeDictData> sysTreeDictDataTree = buildTreeData(sysTreeDictDatas);
        return sysTreeDictDataTree.stream().map(DicTreeSelect::new).collect(Collectors.toList());
    }

    public List<SysTreeDictData> buildTreeData(List<SysTreeDictData> sysTreeDictDatas) {
        List<SysTreeDictData> result = new ArrayList<SysTreeDictData>();
        Set<Long> dataIdSet = sysTreeDictDatas.stream().map(SysTreeDictData::getDataId).collect(Collectors.toSet());
        Map<Long, List<SysTreeDictData>> parentIdMap2SysTreeDictData = sysTreeDictDatas.stream().collect(Collectors.groupingBy(SysTreeDictData::getParentId));
        for (SysTreeDictData sysTreeDictData : sysTreeDictDatas) {
            if(!dataIdSet.contains(sysTreeDictData.getParentId())) {
                // 根节点
                result.add(sysTreeDictData);
            }
            List<SysTreeDictData> children = parentIdMap2SysTreeDictData.get(sysTreeDictData.getDataId());
            sysTreeDictData.setChildren(children);
        }
        return result ;
    }
}
